library(ggplot2)
# Define the data layer
p <- ggplot(data = mtcars, aes(x = wt, y = mpg))
p-1.png)
Used to display changes or trends over time. They are particularly effective for showing the relationship between two continuous variables.
Define the dataset you are going to use and map your aesthetics—this includes specifying which variables will be plotted on the x and y axes.
Add the geometric object that represents the type of plot you want to create. For line graphs, this is geom_line().
For line graphs, this might not always be applicable unless you’re smoothing data. However, for completeness:
The scale layer controls how data values are converted into visual properties. You can customize scales for each aesthetic (e.g., x, y).
While the default is Cartesian coordinates, you can adjust this if needed. For line graphs, typically, the default is sufficient. If you want to flip coordinates you can use coord_flip().
If you wish to create multiple line graphs based on another variable (say, by the number of cylinders), you can add facets.
Finally, you can customize the non-data appearance of your plot using themes.
Add titles, subtitles, captions, and axis labels.